package logic

import (
	"context"
	"errors"
	"novel/models/mysql"

	"novel/novelrpc/internal/svc"
	"novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type BookShelfAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBookShelfAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BookShelfAddLogic {
	return &BookShelfAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 书架添加
func (l *BookShelfAddLogic) BookShelfAdd(in *novelrpc.BookShelfAddRequest) (*novelrpc.BookShelfAddResponse, error) {
	// todo: add your logic here and delete this line
	var firstNovel mysql.User
	err := firstNovel.FirstNovelByUserId(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("用户不存在,不允许添加")
	}
	var bookshelf mysql.Bookshelf
	err = bookshelf.FirstBookshelf(mysql.DB, in)
	if err != nil {
		return nil, err
	}
	if bookshelf.ID != 0 {
		return nil, errors.New("用户已添加,无序添加")
	}
	bookshelfAdd := &mysql.Bookshelf{
		UserId:  in.UserId,
		NovelId: in.NovelId,
	}

	if err = bookshelfAdd.BookSelfAdd(mysql.DB); err != nil {
		return nil, errors.New("添加失败")
	}
	return &novelrpc.BookShelfAddResponse{
		BookshelfId: int64(bookshelfAdd.ID),
	}, nil
}
